/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.codebehind;

import core.BoxNode;
import core.Node;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import view.GUI.LabelReader;

/**
 *
 * @author Jelle
 */
public class CodeModel extends Observable implements Serializable {

    private ArrayList<Node> nodes;
    private HashMap<String, Node> link;

    public CodeModel() {
        nodes = new ArrayList<Node>();
        link = new HashMap<String, Node>();
    }

    public ArrayList<Node> getObjects(String type) {
        ArrayList<Node> set = new ArrayList<Node>();
        try {
            if (type.equals("all")) {
                for (Node c : getObjects()) {
                    set.add(c);
                }
            } 
        } catch (Exception ex) {
            System.out.println(LabelReader.getInstance().getString("CodeModelGetObjectsEXCEPTION") + " "+ex.getMessage());
        }
        return set;
    }

        private ArrayList<Node> getAll() {
        ArrayList<Node> all = new ArrayList<Node>();
        for (Node h : getObjects()) {
            if (BoxNode.class.isAssignableFrom(h.getClass())) {
                BoxNode bn = (BoxNode) h;
                for (Node gg : bn.getInternalNodes().values()) {
                    all.add(gg);
                }
            }
        }
        all.addAll(getObjects());
        return all;
    }

    public void removeObject(Node c) {
        String del = null;
        for(String s : getLink().keySet()){
            Node nn = getLink().get(s);
            if(c.equals(nn)){
                del = s;
            }
        }
        link.remove(del);
        getObjects().remove(c);
        setChanged();
        notifyObservers();
    }

    public void addObject(Node c) {
        nodes.add(c);
        link.put(c.getId(), c);
        setChanged();
        notifyObservers();
    }

     public void printAll() {
        System.out.print(LabelReader.getInstance().getString("CODEALL") + " ");
        for (Node g: getAll()){
            System.out.print("\n\t"+g.hashCode()+"-"+g.getClass().getSimpleName());
        }
        System.out.println("\n");
    }

     public void printObjects() {
        System.out.print(LabelReader.getInstance().getString("CODEOBJECTS") + " ");
        for (Node g: getObjects()){
            System.out.print("\n\t"+g.hashCode()+"-"+g.getClass().getSimpleName());
        }
        System.out.println("\n");
    }
     public void printLinks() {
        System.out.print(LabelReader.getInstance().getString("CODELINKS") + " ");
        for (String g: getLink().keySet()){
            System.out.print("\n\t"+g+"-"+g.hashCode()+"-"+getLink().get(g).getClass().getSimpleName());
        }
        System.out.println("\n");
    }

//    public void editNode(StdNode node, Object value, String parameter) {
//        for (StdNode ni : getObjects("node")) {
//            StdNode n = (StdNode) ni;
//            if (n.equals(node)) {
//                if (parameter.equals("name")) {
//                    n.setName((String)value);
//                } else if (parameter.equals("type")) {
//                    n.setType((String)value);
//                }
//            }
//        }
//        setChanged();
//        notifyObservers();
//    }

    public void removeAll() {
        getObjects().clear();
        getLink().clear();
        setChanged();
        notifyObservers();
    }

   
    public void setModelChanged() {
        setChanged();
    }

    protected void adjustView() {
        setChanged();
        notifyObservers();
    }


    /**
     * @return the link
     */
    public String getLinkString(Node n) {
        String k = null;
        for(String s : getLink().keySet()){
            Node nn = getLink().get(s);
            if(nn.equals(n)){
                k = s;
            }
        }
        return k;
    }

    public Node getLinkNode(String s) {
        return getLink().get(s);
    }

    /**
     * @return the objects
     */
    public ArrayList<Node> getObjects() {
        return nodes;
    }

    /**
     * @param objects the objects to set
     */
    public void setObjects(ArrayList<Node> objects) {
        this.nodes = objects;
    }

    /**
     * @return the link
     */
    public HashMap<String, Node> getLink() {
        return link;
    }

    /**
     * @param link the link to set
     */
    public void setLink(HashMap<String, Node> link) {
        this.link = link;
    }
}
